package com.shenzw.common.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.shenzw.common.aop.anno.ResponseResult;
import com.shenzw.common.controller.response.UserInfoResponse;
import com.shenzw.common.pojo.TestPojo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RScoredSortedSet;
import org.redisson.api.RedissonClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;

/**
 * 测试类
 *
 * @author shenzw
 * @date 2023/10/30
 */
@Slf4j
@RestController
@ResponseResult
@RequestMapping("/test")
public class TestController {

    @Resource
    private RedissonClient redissonClient;


    /**
     * 获取用户信息【演示返回成功案例】
     *
     * @return UserInfo
     */
    @RequestMapping("/getUserInfoSuccess")
    public UserInfoResponse getUserInfoSuccess() {
        log.info("TestController getUserInfo enter...");
        log.debug("debug 日志...");
        log.warn("warn 日志...");
        log.error("error 日志...");

        return UserInfoResponse.builder()
                .name("申忠武")
                .address("广西省桂林市彰泰城市一号6栋一单元")
                .birthday(LocalDate.of(1995, 12, 10))
                .build();
    }

    /**
     * 获取用户信息【演示返回失败案例】
     *
     * @return UserInfo
     */
    @RequestMapping("/getUserInfoFailed")
    public UserInfoResponse getUserInfoFailed() {
        log.info("TestController getUserInfo enter...");

        int a = 1 / 0;
        return UserInfoResponse.builder()
                .name("申忠武")
                .address("广西省桂林市彰泰城市一号6栋一单元")
                .birthday(LocalDate.of(1995, 12, 10))
                .build();
    }

    @GetMapping("/testRedis")
    public void testRedis() {
        Long value = 267L;
        Long value2 = 268L;
        String scoreRedisKey = String.join(":"
                , "CAMPAIGN", "hby".toUpperCase()
                , "test");
        RScoredSortedSet<Object> scoredSortedSet = redissonClient.getScoredSortedSet(scoreRedisKey);

        double score = -10;
        scoredSortedSet.addScore(value, score); // 加入缓存
        scoredSortedSet.addScore(value2, score); // 加入缓存
        log.info("scoreRedisKey = {}，score = {}", scoreRedisKey, score);

        // 删除 redis key 对应的 value
        System.out.println(scoredSortedSet.remove(value));
    }

    /**
     * 测试布隆过滤器
     */
    @GetMapping("/testBloomFilter")
    public void testBloomFilter() {

        // 创建布隆过滤器
        RBloomFilter<String> bloomFilter = redissonClient.getBloomFilter("bloom-filter");
        // 初始化布隆过滤器, 数组长度 100W, 误判率 1%
        bloomFilter.tryInit(1_000_000L, 0.01);
        // 添加数据
        bloomFilter.add("申忠武");

        // 判断是否存在
        System.out.println("[测试存在吗]是否可能存在 = " + bloomFilter.contains("测试存在吗"));
        System.out.println("[申忠武]是否可能存在 = " + bloomFilter.contains("申忠武"));
    }


    /**
     * 测试脱敏
     * <p>
     * {
     * "result": true,
     * "code": 200,
     * "message": "操作成功",
     * "data": {
     * "phone": "182****5066",
     * "password": "************",
     * "address": "***桂林市彰泰城市一号6栋一单元"
     * }
     * }
     *
     * @return TestPojo
     */
    @GetMapping("/testDesensitization")
    public TestPojo testDesensitization() {
        return TestPojo.builder()
                .phone("18273735066")
                .password("SZW123456789")
                .address("广西省桂林市彰泰城市一号6栋一单元")
                .build();
    }

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = simpleDateFormat.parse("2023-12-26 23:49:00");
        long betweenSecond = DateUtil.between(parse, new Date(), DateUnit.SECOND); // 游戏开始时间（秒数）

        System.out.println("beginSecond = " + betweenSecond);
        if (betweenSecond > 50) {
            System.out.println("illegality param.");
        }

        try {
            int i = 1 / 0;
        } catch (Exception e) {
            e.printStackTrace();
//            log.info("错误", e);
        }
    }
}
